package net.gxtvsgwt.client.app;

import java.util.ArrayList;
import java.util.List;

import net.gxtvsgwt.client.app.view.AppView;
import net.gxtvsgwt.client.demo.Demo;
import net.gxtvsgwt.client.demo.type.ButtonDemo;
import net.gxtvsgwt.client.demo.type.CheckBoxDemo;
import net.gxtvsgwt.client.demo.type.GridDemo;
import net.gxtvsgwt.client.demo.type.LabelDemo;
import net.gxtvsgwt.client.demo.type.LargeMixDemo;
import net.gxtvsgwt.client.demo.type.ListBoxDemo;
import net.gxtvsgwt.client.demo.type.MediumMixDemo;
import net.gxtvsgwt.client.demo.type.RadioButtonDemo;
import net.gxtvsgwt.client.demo.type.SmallMixDemo;
import net.gxtvsgwt.client.demo.type.TextBoxDemo;
import net.gxtvsgwt.client.demo.type.TreeDemo;
import net.gxtvsgwt.client.demo.type.VerticalPanelDemo;

import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;

@Singleton
public class AppPresenterImpl implements AppPresenter {

	@Inject
	private Provider<AppView> viewProvider;

	private AppView view;

	private List<Demo> demos;

	public AppPresenterImpl() {
		createDemos();
	}

	@Override
	public AppView getView() {
		if (view == null) {
			view = viewProvider.get();
			List<String> types = new ArrayList<String>();
			for (Demo demo : demos) {
				types.add(demo.getName());
			}
			view.setTypes(types);
		}
		return view;
	}

	@Override
	public void run() {
		view.reset();
		Scheduler.get().scheduleDeferred(new ScheduledCommand() {
			@Override
			public void execute() {
				final Demo demo = demos.get(view.getSelectedIndex());
				final String name = demo.getName();
				final int dataSize = view.getDataSize();
				demo.setDataSize(dataSize);
				Scheduler.get().scheduleDeferred(new ScheduledCommand() {
					@Override
					public void execute() {
						view.setMessage("Running GXT...");
						Scheduler.get().scheduleDeferred(
								new ScheduledCommand() {
									@Override
									public void execute() {
										final long gxtDuration = view.showDemo(
												demo, true);
										Scheduler.get().scheduleDeferred(
												new ScheduledCommand() {
													@Override
													public void execute() {
														view.setMessage("Running GWT...");
														Scheduler
																.get()
																.scheduleDeferred(
																		new ScheduledCommand() {
																			@Override
																			public void execute() {
																				final long gwtDuration = view
																						.showDemo(
																								demo,
																								false);
																				Scheduler
																						.get()
																						.scheduleDeferred(
																								new ScheduledCommand() {
																									@Override
																									public void execute() {
																										view.showResult(
																												name,
																												dataSize,
																												gxtDuration,
																												gwtDuration);
																									}
																								});
																			}
																		});
													}
												});
									}
								});
					}
				});
			}
		});
	}

	protected void createDemos() {
		demos = new ArrayList<Demo>();
		demos.add(new VerticalPanelDemo());
		demos.add(new ButtonDemo());
		demos.add(new CheckBoxDemo());
		demos.add(new RadioButtonDemo());
		demos.add(new LabelDemo());
		demos.add(new TextBoxDemo());
		demos.add(new ListBoxDemo());
		demos.add(new GridDemo());
		demos.add(new TreeDemo());
		demos.add(new SmallMixDemo());
		demos.add(new MediumMixDemo());
		demos.add(new LargeMixDemo());
	}
}
